package com.eastfair.venuebooking.controller;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.eastfair.annotation.annotation.base.BaseControllerInfo;
import com.eastfair.annotation.annotation.log.SysLog;
import com.eastfair.boot.request.PageParams;
import com.eastfair.common.api.*;
import com.eastfair.core.base.R;
import com.eastfair.core.exception.BizException;
import com.eastfair.pay.demand.InvoiceApplyGroup;
import com.eastfair.pay.dto.*;
import com.eastfair.pay.enums.InvoiceApplyBusinessTypeEnum;
import com.eastfair.pay.vo.*;
import com.eastfair.venuebooking.demand.ContractManageGroup;
import com.eastfair.venuebooking.enumeration.ExhibitionStateEnum;
import com.eastfair.venuebooking.exceptioncode.VenueBookingExceptionCode;
import com.eastfair.venuebooking.service.ExhibitionService;
import com.eastfair.venuebooking.service.PayBusinessService;
import com.eastfair.venuebooking.vo.ContractManageVO;
import com.eastfair.venuebooking.vo.SettlementTotalVO;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;


import javax.annotation.Resource;
import javax.validation.constraints.NotNull;
import java.util.List;

/**
 * 场馆关于支付业务
 */

@Slf4j
@RestController
@RequestMapping("/payBusiness")
public class PayBusinessController {

    /**
     * 结算service
     */
    @Autowired
    SettlementFeign settlementFeign;
    /**
     * 业务service
     */
    @Autowired
    PayBusinessService payBusinessService;
    /**
     * 发票申请service
     */
    @Autowired
    InvoiceApplyFeign invoiceApplyFeign;
    /**
     * 发票抬头
     */
    @Autowired
    InvoiceTitleFeign invoiceTitleFeign;
    /**
     * 展会service
     */
    @Resource
    ExhibitionService exhibitionService;
    /**
     * 发票service
     */
    @Autowired
    InvoiceFeign invoiceFeign;
    /**
     * 退款service  1 未支付 2 部分支付 3已支付 4已逾期',  支付方式
     */
    @Autowired
    ApplyRefundFeign applyRefundFeign;

    @Value("${mice-venue-booking.server-name}")
    private String venueServiceName;

    @Value("${mice-venue-booking.invoice.callback-api}")
    private String venueInvoiceCallbackApi;

    @Value("${mice-pay.server-name}")
    private String payServiceName;

    @Value("${mice-pay.invoice.callback-api}")
    private String payInvoiceCallbackApi;
    /**
     * 结算查询前置
     * @param exhibitionId
     * @return
     */
    @GetMapping("/settlementQuery")
    public R<SettlementTotalVO>  settlementQuery(@RequestParam("exhibitionId") @Validated @NotNull(message = "展会ID不允许为空") Long exhibitionId){
        log.info("结算数据查询 入参 exhibitionId {} ",exhibitionId);

        return  R.success(payBusinessService.settlementQuery(exhibitionId));
    }
    /**
     * 结算
     */
    @SysLog("添加结算")
    @PostMapping("/exSettlementSave")
    public R settlementSave(@RequestBody @Validated SettlementSaveDTO saveDTO){
        log.info("settlementSave - 展会结算   saveDTO: {} ", saveDTO);
        //TODO 需要加分布式事务
        try {
            R r = settlementFeign.settlementSave(saveDTO);
            if (r.getIsSuccess()) {
                Boolean aBoolean = exhibitionService.updateExhibitionState(saveDTO.getExhibitionId(), ExhibitionStateEnum.STATE_SEVEN);
                return aBoolean?R.success():R.fail(VenueBookingExceptionCode.EX_SETTLE_FALSE.getMsg());
            } else {
                return R.fail(VenueBookingExceptionCode.EX_SETTLE_FALSE.getMsg());
            }
        } catch (Exception e) {
            log.error("展会结算失败", e);
            return R.fail(VenueBookingExceptionCode.EX_SETTLE_FALSE.getMsg());
        }
    }
    /**
     * 申请开票前置查询、根据合同ID查询
     */
    @GetMapping("/queryContractManageById")
    @BaseControllerInfo(voGroup = {ContractManageGroup.queryContractManageById.class})
    public R<ContractManageVO> queryContractManageById(@RequestParam("contractManageId") @Validated @NotNull(message = "合同ID不能为为空") Long contractManageId){

        return  R.success(payBusinessService.queryContractManageById(contractManageId));
    }
    /**
     * 发票申请保存
     */
    @SysLog("添加发票申请")
    @PostMapping("/saveInvoiceApply")
    public R saveInvoiceApply(@RequestBody InvoiceApplyDTO invoiceApplyDTO) {
        if (invoiceApplyDTO.getBusinessType().eq(InvoiceApplyBusinessTypeEnum.J)) {
            invoiceApplyDTO.setCallbackServiceName(payServiceName);
            invoiceApplyDTO.setCallbakcApi(payInvoiceCallbackApi);
        } else {
            invoiceApplyDTO.setCallbackServiceName(venueServiceName);
            invoiceApplyDTO.setCallbakcApi(venueInvoiceCallbackApi);
        }
        R r = invoiceApplyFeign.saveInvoiceApply(invoiceApplyDTO);
        if (r.getIsSuccess()) {
            return r;
        } else {
            throw BizException.wrap(r.getCode(), r.getErrorMsg());
        }
    }

    /**
     * 发票抬头查询
     */
    @PostMapping("/queryInvoiceTitle")
    public R<List<InvoiceTitleVO>> queryInvoiceTitle(@RequestBody InvoiceTitleDTO invoiceTitleDTO){
        R<List<InvoiceTitleVO>> r = invoiceTitleFeign.queryInvoiceTitle(invoiceTitleDTO);
        if (r.getIsSuccess()) {
            return r;
        } else {
            throw BizException.wrap(r.getCode(), r.getErrorMsg());
        }

    };
    /**
     * 发票申请查询
     */
    @PostMapping("/invoiceApplyQueryList")
    public R<Page<InvoiceApplyVO>> invoiceApplyQueryList(@RequestBody @Validated PageParams<InvoiceApplyPageQuery> params) {
        R<Page<InvoiceApplyVO>> r = invoiceApplyFeign.queryList(params);
        if (r.getIsSuccess()) {
            return r;
        } else {
            throw BizException.wrap(r.getCode(), r.getErrorMsg());
        }
    }

    /**
     * 合同发票申请详情记录查询
     */
    @PostMapping("/queryInvoicecList")
    public R<List<InvoiceVO>> queryInvoicecList(@RequestBody InvoiceDTO invoiceDTO) {

        return invoiceFeign.queryInvoicecList(invoiceDTO);
    }

    /**
     * 结算列表查询
     */
    @PostMapping("/settlementQueryList")
    public R<Page<SettlementVO>> settlementQueryList(@RequestBody @Validated PageParams<SettlementPageQuery> params){

        return settlementFeign.queryList(params);
    }
    /**
     *结算列表详情查询
     */
    @GetMapping("/settlementQueryById")
    public R<SettlementVO>  settlementQueryById(@RequestParam("settlementId") @Validated @NotNull(message = "结算单ID不能为为空") Long settlementId){
        log.info("结算详情查询 入参 settlementId {} ",settlementId);

        return  settlementFeign.settlementQueryById(settlementId);
    }
    /**
     * 结算列表退款确认
     */
    @SysLog("结算退款确认")
    @PostMapping("/saveApplyRefund")
    public R saveApplyRefund(@RequestBody ApplyRefundDTO applyRefundDTO) {
        return applyRefundFeign.saveApplyRefund(applyRefundDTO);
    }

    /**
     * 退款管理查询
     *
     * @return
     */
    @PostMapping("/applyRefund/queryList")
    public R<Page<ApplyRefundVO>> queryList(@RequestBody @Validated PageParams<ApplyRefundPageQuery> params) {

        return applyRefundFeign.queryList(params);
    }
    /**
     * 退款详情查询
     *
     * @param applyRefundId
     * @return
     */
    @GetMapping("applyRefundQueryById")
    public R<ApplyRefundVO> applyRefundQueryById(@RequestParam("applyRefundId") @Validated @NotNull(message = "退款ID不能为为空") Long applyRefundId) {
        log.info("退款详情查询 入参 applyRefundId {} ", applyRefundId);

        return applyRefundFeign.applyRefundQueryById(applyRefundId);
    }

    /**
     * 确认退款
     */
    @SysLog("添加退款确认")
    @PostMapping("/saveApplyRefundCnfirm")
    public R saveApplyRefundCnfirm(@RequestBody ApplyRefundDTO applyRefundDTO) {
        return applyRefundFeign.saveApplyRefundCnfirm(applyRefundDTO);
    }

    /**
     * 确认开票详情查询
     * @param invoiceApplyId
     * @return
     */
    @GetMapping("invoiceApplyCnfirmQueryById")
    @BaseControllerInfo(voGroup = {InvoiceApplyGroup.invoiceApplyCnfirmQueryById.class})
    public R<InvoiceApplyVO>  invoiceApplyCnfirmQueryById(@RequestParam("invoiceApplyId") @Validated @NotNull(message = "发票ID不能为为空") Long invoiceApplyId){
        log.info("结算详情查询 入参 invoiceApplyId {} ",invoiceApplyId);

        return invoiceApplyFeign.invoiceApplyCnfirmQueryById(invoiceApplyId);
    }

    /**
     * 开票详情查询
     * @param invoiceApplyId
     * @return
     */
    @GetMapping("invoiceApplyQueryById")
    @BaseControllerInfo(voGroup = {InvoiceApplyGroup.invoiceApplyVOQueryById.class})
    public R<InvoiceApplyVO>  invoiceApplyQueryById(@RequestParam("invoiceApplyId") @Validated @NotNull(message = "发票ID不能为为空") Long invoiceApplyId){
        log.info("结算详情查询 入参 invoiceApplyId {} ",invoiceApplyId);

        return  invoiceApplyFeign.invoiceApplyQueryById(invoiceApplyId);
    }

    /**
     * 确认开票
     */
    @SysLog("确认开票")
    @PostMapping("/saveInvoicecCnfirm")
    public R saveInvoicecCnfirm(@RequestBody List<InvoiceDTO> invoiceDTO) {

        return invoiceFeign.saveInvoicecCnfirm(invoiceDTO);
    }
}
